package com.ikas.ai.server.module.risk.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.server.error.BusinessException;
import com.ikas.ai.server.module.risk.dao.RiskInspectionDAO;
import com.ikas.ai.server.module.risk.model.RiskInspection;
import com.ikas.ai.server.module.risk.model.dto.RiskInspectionQueryDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 风险巡检项管理服务类
 *
 * @author tang.xuandong
 * @version 1.0.0
 * @date 2023/1/31 13:57
 */
@Service
public class RiskInspectionService extends ServiceImpl<RiskInspectionDAO, RiskInspection> {

    @Autowired
    private RiskInspectionDAO riskInspectionDAO;

    /**
     * 分页查询
     *
     * @param req 风险巡检项分页DTO
     * @return list
     */
    public List<RiskInspection> pageQueryRiskInspection(RiskInspectionQueryDto req) {
        LambdaQueryChainWrapper<RiskInspection> lambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(riskInspectionDAO);
        return lambdaQueryChainWrapper
                .like(StringUtils.isNotBlank(req.getName()), RiskInspection::getName, req.getName())
                .eq(StringUtils.isNotBlank(req.getMachineNo()), RiskInspection::getMachineNo, req.getMachineNo())
                .eq(StringUtils.isNotBlank(req.getEquipmentCode()), RiskInspection::getEquipmentCode, req.getEquipmentCode())
                .eq(StringUtils.isNotBlank(req.getSystemNo()), RiskInspection::getSystemNo, req.getSystemNo())
                .list();
    }

    /**
     * 查询风险巡检项，通过设备编码
     *
     * @param equipmentCode 设备编码
     * @return List<RiskInspection>
     */
    public List<RiskInspection> getRiskInspectionByEquipmentCode(String equipmentCode) {
        LambdaQueryChainWrapper<RiskInspection> lambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(riskInspectionDAO);
        return lambdaQueryChainWrapper
                .eq(StringUtils.isNotBlank(equipmentCode), RiskInspection::getEquipmentCode, equipmentCode)
                .list();
    }

    /**
     * 查询风险巡检项，同步系统编号
     *
     * @param systemNo 系统编号
     * @return List<RiskInspection>
     */
    public List<RiskInspection> getRiskInspectionBySystemNo(String systemNo) {
        LambdaQueryChainWrapper<RiskInspection> lambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(riskInspectionDAO);
        return lambdaQueryChainWrapper
                .eq(RiskInspection::getSystemNo, systemNo)
                .list();
    }

    /**
     * 新增风险巡检项
     *
     * @param param RiskInspection
     */
    public int saveRiskInspection(RiskInspection param) {
        String name = param.getName();
        String equipmentCode = param.getEquipmentCode();
        String systemNo = param.getSystemNo();
        if (StringUtils.isBlank(name) || StringUtils.isBlank(equipmentCode) || systemNo == null) {
            throw new BusinessException("巡检项目、设备编码、系统编号不能为空");
        }
        param.setCreateTime(new Date());
        return riskInspectionDAO.insert(param);
    }

    /**
     * 编辑风险巡检项
     *
     * @param param RiskInspection
     * @return row
     */
    public int updateRiskInspection(RiskInspection param) {
        Long id = param.getId();
        String name = param.getName();
        String equipmentCode = param.getEquipmentCode();
        String systemNo = param.getSystemNo();
        if (id == null) {
            throw new BusinessException("ID不能为空");
        }
        if (StringUtils.isBlank(name) || StringUtils.isBlank(equipmentCode) || systemNo == null) {
            throw new BusinessException("巡检项目、设备编码、系统编号不能为空");
        }
        param.setUpdateTime(new Date());
        return riskInspectionDAO.updateById(param);
    }

    /**
     * 删除风险巡检项
     *
     * @param ids list
     */
    public int deleteRiskInspection(List<Long> ids) {
        return riskInspectionDAO.deleteBatchIds(ids);
    }
}
